package com.sihuatech.coreagent.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import com.google.common.collect.Maps;
import com.sihuatech.coreagent.core.communication.entity.PageInfo;
import com.sihuatech.coreagent.core.communication.entity.RequestSetting;
import com.sihuatech.coreagent.core.communication.entity.Response;
import com.sihuatech.coreagent.mapper.RequestSettingMapper;
import com.sihuatech.coreagent.service.RequestSettingService;
import com.sihuatech.coreagent.until.Consts;

@RestController
@RequestMapping("coreagent/requestSetting")
public class RequestSettingController {

	private static final Logger logger = LoggerFactory.getLogger(RequestSettingController.class);
	@Autowired
	private RequestSettingService requestSettingService;

	@Autowired
	private RequestSettingMapper requestSettingMapper;

	/***
	 * 保存请求配置
	 * 
	 * @param requestSetting
	 * @return
	 */
	@PostMapping("/saveRequestSetting")
	public Response saveRequestSetting(@RequestBody RequestSetting requestSetting) {
		Response res = new Response();
		if (requestSetting == null) {
			logger.error("需要保存的请求配置对象为空");
			res.failWithMessage("没有需要保存的请求配置");
			return res;
		}
		res = requestSettingService.saveRequestSetting(requestSetting, res);

		return res;
	}

	@PostMapping("/getAllByPage")
	public Object getAllRequestSetting(@RequestParam(required = true) Integer size,
			@RequestParam(required = true) Integer currentPage, @RequestBody RequestSetting requestSetting) {
		QueryWrapper<RequestSetting> qw = new QueryWrapper<>();
		QueryWrapper<RequestSetting> qw1 = new QueryWrapper<>();
		if (!StringUtils.isEmpty(requestSetting.getPlatformCode())) {
			qw.eq("platform_code", requestSetting.getPlatformCode());
			qw1.eq("platform_code", requestSetting.getPlatformCode());
		}
		if (!StringUtils.isEmpty(requestSetting.getPlatformUrl())) {
			qw.eq("platform_url", requestSetting.getPlatformUrl());
			qw1.eq("platform_url", requestSetting.getPlatformUrl());
		}
		if (!StringUtils.isEmpty(requestSetting.getProvinceCode())) {
			qw.eq("province_code", requestSetting.getProvinceCode());
			qw1.eq("province_code", requestSetting.getProvinceCode());
		}
		if (!StringUtils.isEmpty(requestSetting.getInterfaceType())) {
			qw.eq("interface_type", requestSetting.getInterfaceType());
			qw1.eq("interface_type", requestSetting.getInterfaceType());
		}

		List<RequestSetting> resultList = requestSettingMapper
				.selectList(qw.orderByDesc("id").last(" limit " + (currentPage - 1) * size + "," + size));
		int total = requestSettingMapper.selectCount(qw1);
		PageInfo<RequestSetting> pageInfo = new PageInfo<>(currentPage, size, total, resultList);
		return pageInfo;
	}

	@GetMapping("/infoRequestSettingById")
	public Response infoRequestSettingById(Long id) {
		Response res = new Response();
		RequestSetting rs = requestSettingMapper.selectById(id);
		if (rs == null) {
			res.failWithMessage("查询失败，id为 ：" + id);
			return res;
		}
		res.setCodeAndMessageAndData(Consts.SUCCESS, "查询成功", rs);
		return res;
	}
	
	@DeleteMapping("/deleteByIds")
	public Object deleteByIds(@RequestBody String[] ids) {
		Response res = new Response();
		if (ids == null || ids.length == 0) {
			res.failWithMessage("没有要删除的请求配置");
			return res;
		}
		try {
			boolean result = requestSettingService.deleteByIds(ids);
			if (!result) {
				res.failWithMessage("需要删除的请求配置不存在");
				return res;
			}
		} catch (Exception e) {
			logger.error("删除请求配置异常", e);
			res.failWithMessage("删除请求配置异常");
			return res;
		}
		res.setCodeAndMessage(Consts.SUCCESS, "删除成功");
		return res;
	}
}
